home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT01.ZIP / temp / techtut / tut1 / tech01.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-09-10  |  7.4 KB  |  216 lines

  1. {
  2.  
  3.         TechTutor1
  4.         Bullets using linked-list technique
  5.  
  6.         Coding by P.Bestebroer
  7.         FreeWare
  8.  
  9.         This source will not work on it's own, it just shows how to
  10.         do bullet-code into you'r game...
  11.  
  12.         Contacting:
  13.  
  14.          HTTP://people.zeelandnet.nl/rpb/
  15.         EMAIL:just4fun@zeelandnet.nl
  16.  
  17. }
  18. PROGRAM Tech01;
  19.  
  20. USES CRT;
  21. {─────────────────────────────────────────────────────────────────────────────}
  22. {
  23.         First create the variables
  24.  
  25. }
  26. TYPE PBullet       = ^TBullet;
  27.      TBullet       = record
  28.         xpos,ypos     : integer;
  29.         xspeed,yspeed : integer;
  30.         f_Frame       : byte;   { image frame }
  31.         ai            : byte;   { bullet AI identifier         }
  32.         subai         : byte;   { person who shot it           }
  33.         prev,next     : Pbullet;{ link to next/previous bullet }
  34.      END;
  35.  
  36. VAR   FirstBullet     : PBullet;  { first bullet in list         }
  37.       LastBullet      : PBullet;  { last bullet in list          }
  38. {─────────────────────────────────────────────────────────────────────────────}
  39. {
  40.         The addbullet procedure will add a bullet to the linked-list.
  41.  
  42.         Expects: Xposition, Yposition, XSpeed, YSpeed
  43.                  Image-Frame number, BULLET-AI, shot-by-ai
  44.         Returns: TRUE if bullet was added to the list.
  45. }
  46. FUNCTION AddBullet(  Xpos2,Ypos2,xspeed2,yspeed2  : integer;
  47.                      F_Frame2,ai2,subai2          : byte):boolean;
  48. VAR  TempBull : PBullet;    { this is used to create the new-bullet }
  49.  
  50. BEGIN
  51.  AddBullet:=false;  { No bullet is added upto this point }
  52.  
  53.  new(TempBull);              { Get memory for new bullet }
  54.  
  55.  if TempBull=nil then exit;  { if TEMP=NIL, we don't have enough memory }
  56.  
  57.  if FirstBullet=NIL then begin { is the list empty? }
  58.     LastBullet:=TempBull;       { yes, so the NEW-BULLET is the first+last }
  59.     FirstBullet:=TempBull;
  60.     with FirstBullet^ do begin
  61.          next:=NIL;             { next/previous are nothing, because there }
  62.          prev:=Nil;             { is only one bullet in the list }
  63.     end;
  64.   end else begin                 { More bullets in the list! }
  65.      LastBullet^.next:=tempbull;{ Lastbullet points to new-bullet }
  66.      TempBull^.prev:=LastBullet;{ lastbullet is not lastbullet anymore }
  67.      LastBullet:=TempBull;      { Lastbullet=NEWBULLET }
  68.      Tempbull^.next:=NIL;       { no bullets after the new-bullet }
  69.   end;
  70.  
  71.   With TempBull^ do begin        { Set the new values }
  72.    xpos:=xpos2+xspeed2;
  73.    ypos:=ypos2+yspeed2;
  74.    xspeed:=xspeed2;
  75.    yspeed:=yspeed2;
  76.  
  77.    ai       :=ai2;
  78.    subai    :=subai2;
  79.    f_Frame  :=f_Frame2;
  80.   end;
  81.  
  82.   AddBullet:=True;               { the bullet was added }
  83. END;
  84. {─────────────────────────────────────────────────────────────────────────────}
  85. {
  86.  
  87.         Erase a BULLET object from the linked-list
  88.  
  89.         Expects: pointer to BULLET object  that needs to be ERASED
  90.         Returns: True if succesfull
  91.  
  92. }
  93. FUNCTION EraseBullet(Temp:PBullet):boolean;
  94. VAR  next_bull,
  95.      prev_bull  : PBullet;
  96. BEGIN
  97.   EraseBullet:=false;           { bullet not yet erased }
  98.   if Temp=NIL then exit;        { if bullet=NIL then just exit }
  99.  
  100.   prev_BULL:=Temp^.prev;        { Correct the bullet pointers }
  101.   next_BULL:=Temp^.next;
  102.  
  103.   if prev_BULL=NIL then begin   { if there is no previous bullet, then }
  104.      FirstBullet:=next_Bull;    { we'r working with the firstbullet }
  105.   end else
  106.      Prev_bull^.next:=next_bull;
  107.  
  108.   if next_bull=NIL then begin   { if there is no next bullet , then }
  109.      LastBullet:=prev_Bull;     { we'r working with the lastbullet }
  110.   end else
  111.      Next_Bull^.prev:=prev_Bull;
  112.  
  113.   dispose(Temp);                { dispose the bullet, freeing up memory }
  114. END;
  115.  
  116. {─────────────────────────────────────────────────────────────────────────────}
  117. {
  118.         The DOBULLETS procedure will process the linked-list
  119.  
  120.         Expects: Nothing
  121.         Returns: Nothing
  122. }
  123. PROCEDURE DoBullets;
  124. VAR temp      : PBullet;        { temporary bullet    }
  125.     next_Bull : PBullet;        { next bullet in list }
  126.     done      : boolean;        { bullet done?        }
  127. BEGIN
  128.  Temp:=FirstBullet;             { start with first bullet }
  129.  
  130.  WHILE Temp<>Nil do begin       { while not pointing to NIL, do bullets }
  131.        with Temp^ do begin
  132.             done:=false;        { bullet not yet done }
  133.             next_Bull:=temp^.next; { pointer to Next-bullet in list }
  134.  
  135.             Case AI of             { check on bullet AI }
  136.              1 : BEGIN             { normal bullet }
  137.                    inc(xpos,xspeed); { increase X position }
  138.                    inc(ypos,yspeed); { increase Y position }
  139.                    {
  140.                      ....
  141.                        Check boundaries of screen
  142.                        and check for wall-background blocks
  143.                      ....
  144.                    }
  145.                    { Bullet hits something, or is off-screen }
  146.                       if not EraseBullet(temp) then begin
  147.                          { ERROR! bullet could not be disposed }
  148.                          halt(1);
  149.                       end;
  150.                       Done:=True; { Bullet is done }
  151.                  END;
  152.             END;{CASE ai }
  153.  
  154.             { if bullet was not "erased" continue the procedure }
  155.             If Not done then begin { see if we hit the player }
  156.                  {
  157.                    ....
  158.                      See if bullet is not shot by player
  159.                    ....
  160.                  }
  161.                  {
  162.                    ....
  163.                     Check with player coordinates
  164.                    ....
  165.                  }
  166.                  { IF HIT PLAYER THEN BEGIN }
  167.                      { Bullet hits player, and is erased }
  168.                      if not EraseBullet(temp) then begin
  169.                         { ERROR! bullet could not be disposed }
  170.                         halt(1);
  171.                      end;
  172.                      done:=true;
  173.             end;
  174.             { if bullet was not "erased" continue the procedure }
  175.             if not done then
  176.                {
  177.                  ....
  178.                    Draw the image on the screen!
  179.                  ....
  180.                }
  181.        end;
  182.        temp:=next_Bull;  { get pointer to next bullet in the list }
  183.  end;
  184. END;
  185. {─────────────────────────────────────────────────────────────────────────────}
  186. BEGIN
  187.   FirstBullet:=Nil;   { No first bullet in list }
  188.    LastBullet:=Nil;   { and no last bullet in list... }
  189.  
  190.   while port[$60]<>156 do ;
  191.   textcolor(7); textbackground(0); clrscr;
  192.   writeln('TechTutor #1');
  193.   writeln('written by P.Bestebroer, Just4Fun Productions');
  194.   writeln('');
  195.   writeln('This topic is for adding bullets to your games, since I don''t');
  196.   writeln('feel like writing a complete example of an implementation in a game');
  197.   writeln('this example wont work on it self.');
  198.   writeln;
  199.   writeln('The only major changes to get this working are in the DOBULLETS procedure');
  200.   writeln('You should use a great VGA unit (SuperFX engine for example ;) and ');
  201.   writeln('implement the things like drawing the bullets etc..');
  202.   writeln;
  203.   writeln('Watch out for the other techtutors...');
  204.   writeln;
  205.   writeln('Press a key');
  206.  
  207.   writeln;
  208.   writeln;
  209.   writeln;
  210.   writeln('----------------------------------');
  211.   writeln('Contacting: just4fun@zeelandnet.nl');
  212.   writeln('http://people.zeelandnet.nl/rpb   ');
  213.   writeln('----------------------------------');
  214.   repeat until port[$60]<>156;
  215. END.
  216.